package pl.rgolcz.antnet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.uci.ics.jung.graph.Graph;

@SuppressWarnings("serial")
public class Main extends JPanel
{
    public static void run(File graphFile, int root, int dest)
    {
        Graph<Integer, IWeightedEdge> graph = null;
        /* Load the graph definition from file */
        try
        {
            graph = getGraph(graphFile);
        }
        catch (IOException e)
        {
            System.out.println("Failed to load the graph");
            e.printStackTrace();
            System.exit(1);
        }

        /* Run Dijkstra algorithm */
        System.out.println("\n-- Running Dijkstra algorithm --");
        DijkstraShortestPath<Integer> dijkstra = new DijkstraShortestPath<>(graph, root);
        dijkstra.run();
        List<Integer> dijkstraShortestPath = dijkstra.getShortestPath(dest);
        System.out.println("Dijkstra shortest path: " + dijkstraShortestPath);
        System.out.println("Dijkstra shortest path length: " + dijkstra.getShortestPathLength(dest));

        /* Run ant algorithm */
        System.out.println("\n-- Running Ant algorithm --");
        AntNet antnet = new AntNet(graph, root, dest);
        antnet.run();
    }

    public static void main(String[] s)
    {

        /* Get input params */

        // graph source [file|random]
        // root vertex
        // dest vertex
        // visualization
        boolean visualization = true;
        int root = 5;
        int dest = 21;
        /*
         * int root = 0;
         * int dest = 6;
         */

        System.out.println("Root vertex: " + root);
        System.out.println("Dest vertex: " + dest);

        Graph<Integer, IWeightedEdge> graph = null;

        /* Load the graph definition from file */
        try
        {
            graph = getGraph(new File("random.net"));
        }
        catch (IOException e)
        {
            System.out.println("Failed to load the graph");
            e.printStackTrace();
            System.exit(1);
        }

        /* Graph visualization */
        if (visualization)
        {
            JFrame jf = new JFrame();
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.getContentPane().add(new Visualize(graph));
            jf.pack();
            jf.setVisible(true);
        }

        /* Run Dijkstra algorithm */
        System.out.println("\n-- Running Dijkstra algorithm --");
        DijkstraShortestPath<Integer> dijkstra = new DijkstraShortestPath<>(graph, root);
        dijkstra.run();
        List<Integer> dijkstraShortestPath = dijkstra.getShortestPath(dest);
        System.out.println("Dijkstra shortest path: " + dijkstraShortestPath);
        System.out.println("Dijkstra shortest path length: " + dijkstra.getShortestPathLength(dest));

        /* Run ant algorithm */
        System.out.println("\n-- Running Ant algorithm --");
        AntNet antnet = new AntNet(graph, root, dest);
        antnet.run();
    }

    /**
     * @return the graph for this demo
     * @throws IOException
     */
    static Graph<Integer, IWeightedEdge> getGraph(File graphFile) throws IOException
    {

        // BufferedReader reader = new BufferedReader(new FileReader(new File("graph.net")));
        BufferedReader reader = new BufferedReader(new FileReader(graphFile));

        GraphReader graphReader = new GraphReader(reader);
        Graph<Integer, IWeightedEdge> graph = graphReader.read();
        return graph;
    }
}
